//package com.markchu.web.structure.controller;
//
//import com.markchu.common.enums.StatusEnum;
//import com.markchu.common.utils.CookieUtils;
//import com.markchu.common.vo.MessageInfosVo;
//import com.markchu.common.vo.ResultVo;
//import com.markchu.component.redis.RedisCache;
//import com.markchu.component.redis.utils.RedisHelper;
//import com.markchu.modules.system.domain.*;
//import com.markchu.modules.system.enums.FileTypeEnum;
//import com.markchu.modules.system.service.*;
//import com.markchu.modules.system.vo.UserVo;
//import com.markchu.web.structure.vo.UserInfoVo;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.domain.Example;
//import org.springframework.data.domain.ExampleMatcher;
//import org.springframework.data.domain.Page;
//import org.springframework.stereotype.Controller;
//import org.springframework.ui.Model;
//import org.springframework.web.bind.annotation.*;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.*;
//
//@Controller
//@RequestMapping("/home")
//public class HomeController_back extends BaseController{
//    @Autowired
//    private UploadService uploadService;
//
//    @Autowired
//    private ActivityService activityService;
//
//    @Autowired
//    private NewsService newsService;
//
//    @Autowired
//    private ActivityUserPicService activityUserPicService;
//
//    @Autowired
//    private ActivityJoinUserService activityJoinUserService;
//
//    @Autowired
//    private RedisCache redisCache;
//
//    @Autowired
//    private ChatsService chatsService;
//
//
//    @GetMapping("/index")
//    public String index(Model model) {
//        Long userId = getUserIdBy();
//        model.addAttribute("userId",userId);
//        return "/home/index";
//    }
//
//    @GetMapping("/release")
//    public String release(Model model) {
//        Long userId = getUserIdBy();
//        model.addAttribute("userId",userId);
//        return "/home/release/release";
//    }
//    @GetMapping("/release/index")
//    public String releaseIndex(Model model) {
//        Long userId = getUserIdBy();
//        model.addAttribute("userId",userId);
//        return "/home/release/index";
//    }
//
//
//    @GetMapping("/hospital")
//    public String hospital(Model model) {
//        Long userId = getUserIdBy();
//        model.addAttribute("userId",userId);
//        return "/home/hospital";
//    }
//
//    @GetMapping("/mymsgs")
//    public String mymsgs(Model model) {
//        Long userId = getUserIdBy();
//        Chats chats = new Chats();
//        chats.setFromUserId(userId);
//        chats.setToUserId(userId);
//        List<UserVo> dataList = chatsService.findAllChats(chats);
////        ExampleMatcher matcher = ExampleMatcher.matching();
////        chats.setStatus(StatusEnum.OK.getCode());
////        Example<Chats> example = Example.of(chats, matcher);
////        Page<Chats> dataList = chatsService.getPageList(chats);
//        model.addAttribute("dataList",dataList);
//        model.addAttribute("fromUserId",userId);
//        return "/home/mymsgs";
//    }
//
//    @GetMapping("/chats/{fromUserId}/{toUserId}")
//    public String mymsgs(Model model,@PathVariable Long fromUserId,@PathVariable Long toUserId,String autoredirect) {
//        model.addAttribute("fromUserId",fromUserId);
//        model.addAttribute("toUserId",toUserId);
//        if(StringUtils.isEmpty(autoredirect)){
//            autoredirect = "/home/mymsgs";
//        }
//        model.addAttribute("autoredirect",autoredirect);
//        return "/home/mymsgs_chat";
//    }
//
//    @GetMapping("/active")
//    public String active(Model model) {
//        Activity activity = new Activity();
//        ExampleMatcher matcher = ExampleMatcher.matching();
//        activity.setStatus(StatusEnum.OK.getCode());
//        Example<Activity> example = Example.of(activity, matcher);
//        List<Activity> resultList = activityService.findList(example);
//        model.addAttribute("resultList",resultList);
//        return "/home/active";
//    }
//
//    @GetMapping("/active/item/{activeId}")
//    public String activeItem(Model model,@PathVariable Long activeId) {
//        Activity activity = activityService.getById(activeId);
//        model.addAttribute("activity",activity);
//
//        //获取参与活动的信息
//        List<Map<String,Object>> dataList = activityUserPicService.findAllByacId(activeId);
//        model.addAttribute("dataList",dataList);
//        Map<String,String> userThumMap = getUserThumByMap(dataList);
//        //获取对应的排名信息
//        List<ActivityJoinUser> topList = activityJoinUserService.getListBy(activeId,"order");
//        getUserInfoList(topList,userThumMap);
//        model.addAttribute("topList",topList);
//
//        model.addAttribute("checkLogin",getUserIdBy());
//        model.addAttribute("autoredirect","/home/active/item/"+activeId);
//        return "/home/activeItem";
//    }
//
//    private Map<String, String> getUserThumByMap(List<Map<String, Object>> dataList) {
//        Map<String,String> userThumMap = new HashMap<>();
//        for(Map<String, Object> dataMap : dataList){
//            String keyCode = String.valueOf(dataMap.get("userId"));
//            String keyValue = String.valueOf(dataMap.get("path"));
//            userThumMap.put(keyCode,keyValue);
//        }
//        return userThumMap;
//    }
//
//    private <T> List<ActivityJoinUser> getUserInfoList(List<ActivityJoinUser> dataList,Map<String,String> userThumMap) {
//        List<Long> userIds = new ArrayList<>();
//        for(ActivityJoinUser activityJoinUser : dataList){
//            userIds.add(activityJoinUser.getUser_id());
//        }
//        Map<Long, UserInfoVo> userIdNickNameMap = getUserIdNickNameMap(userIds);
//        for(ActivityJoinUser activityJoinUser : dataList){
//            UserInfoVo userInfoVo = userIdNickNameMap.get(activityJoinUser.getUser_id());
//            String nickName = userInfoVo.getNickName();
//            if(StringUtils.isNotEmpty(nickName) && nickName.length()>7){
//                nickName = nickName.substring(0,6)+"...";
//            }
//            activityJoinUser.setUserNickName(nickName);
//            activityJoinUser.setUserThum(userInfoVo.getUserThum());
//        }
//        return dataList;
//    }
//
//    @GetMapping("/active/join/{activeId}")
//    public String join(Model model,@PathVariable Long activeId) {
//        Activity activity = activityService.getById(activeId);
//        model.addAttribute("activity",activity);
//
//        User user = getUserInfo();
//        if(Objects.equals(user,null)){
//            model.addAttribute("autoredirect","/active/join/"+activeId);
//            return "/home/login";
//        }
//        ExampleMatcher matcher = ExampleMatcher.matching();
//        Upload upload = new Upload();
//        upload.setUserId(user.getId());
//        upload.setFileType(FileTypeEnum.WEB.getCode());
//        upload.setStatus(Long.valueOf(StatusEnum.OK.getCode()));
//        Example<Upload> example = Example.of(upload, matcher);
//        Page<Upload> uploadPage = uploadService.getPageList(example);
//        model.addAttribute("photoList",uploadPage.getContent());
//        model.addAttribute("page", uploadPage);
//        return "/home/join";
//    }
//
//    @GetMapping("/news")
//    public String news(Model model) {
//        ExampleMatcher matcher = ExampleMatcher.matching();
//        News news = new News();
//        news.setStatus(StatusEnum.OK.getCode());
//        Example<News> example = Example.of(news, matcher);
//        Page<News> uploadPage = newsService.getPageList(example);
//        model.addAttribute("dataList",uploadPage.getContent());
//        model.addAttribute("page", uploadPage);
//        return "/home/news";
//    }
//
//    @GetMapping("/news/{newsId}")
//    public String news(Model model,@PathVariable Long newsId) {
//        News news = newsService.getById(newsId);
//        model.addAttribute("news",news);
//        return "/home/newsDetails";
//    }
//
//    @GetMapping("/userInfo")
//    public String userInfo(Model model) throws IOException {
//        User user = getUserInfo();
//        if(StringUtils.isEmpty(user.getAddress())){
//            model.addAttribute("location",getLocationByIp());
//        }else{
//            model.addAttribute("location",user.getAddress());
//        }
//        model.addAttribute("user",user);
//        return "/home/userInfo";
//    }
//
//
//    @GetMapping("/login")
//    public String login(String autoredirect,Model model,HttpServletResponse response) throws IOException {
//        User user = getUserInfo();
//        if(!Objects.equals(user,null)){
//            response.sendRedirect("/home/myhome");
//        }
//        model.addAttribute("login",true);
//        model.addAttribute("autoredirect",autoredirect);
//        return "/home/login";
//    }
//
//    @GetMapping("/myhome")
//    public String myhome(Model model, HttpServletResponse response) throws IOException {
//        User user = getUserInfo();
//        if(Objects.equals(user,null)){
//           user = new User();
//        }
//        model.addAttribute("user",user);
//        return "/home/myhome";
//    }
//
//    @GetMapping("/mydc")
//    public String mydc(Model model) throws IOException {
//        Long userId = getUserIdBy();
//        //我上传的图片信息
//        ExampleMatcher matcher = ExampleMatcher.matching();
//        Upload upload = new Upload();
//        upload.setUserId(userId);
//        upload.setFileType(FileTypeEnum.WEB.getCode());
//        upload.setStatus(Long.valueOf(StatusEnum.OK.getCode()));
//        Example<Upload> example = Example.of(upload, matcher);
//        Page<Upload> uploadPage = uploadService.getPageList(example);
//        model.addAttribute("photoList",uploadPage.getContent());
//        model.addAttribute("page", uploadPage);
//        return "/home/mydc";
//    }
//
//
//    @GetMapping("/upload/{imgId}")
//    public String upload(Model model,@PathVariable Long imgId) {
//        Upload upload = uploadService.findById(imgId);
//        model.addAttribute("upload",upload);
//        return "/home/upload";
//    }
//
//    @PostMapping("/publish/{imgId}")
//    @ResponseBody
//    public ResultVo publish(@PathVariable Long imgId, String remark,String autoredirect, HttpServletRequest request) {
//        ResultVo resultVo = new ResultVo();
//        Upload upload = uploadService.findById(imgId);
//        String session_code = CookieUtils.getCookieValue(request,MessageInfosVo.SESSION_CODE);
//        if(StringUtils.isEmpty(session_code) || Objects.equals(session_code,null)){
//            resultVo.setCode(9999);
//            return resultVo;
//        }
//        Long userId = Long.valueOf(RedisHelper.get(session_code));//.set(keyCode,String.valueOf(user.getId()));
//        if(Objects.equals(upload,null) || Objects.equals(userId,null)){
//            resultVo.setCode(500);
//            resultVo.setMsg("发布失败......!");
//
//        }
//        upload.setRemark(remark);
//        upload.setUserId(userId);
//        upload.setStatus(Long.valueOf(StatusEnum.OK.getCode()));
//        uploadService.updateUpload(upload);
//        resultVo.setCode(200);
//        resultVo.setAutoredirect(autoredirect);
//        return resultVo;
//    }
//
//
//
//
//
//    @GetMapping("/test")
//    public String test(Model model) {
//        redisCache.setCacheObject("1","2");
//        System.out.println("---");
//        Object o = redisCache.getCacheObject("1");
//        System.out.println(String.valueOf(o));
//
//        Long lo = RedisHelper.incr("1");
//        if(!RedisHelper.exists("keyCode_expire")){
//            RedisHelper.expire("1",20);
//        }
//        RedisHelper.set("keyCode_expire","keyCode_expire");
//        System.out.println("lo"+lo);
//        return "/home/test";
//    }
//
//
//
//
//
//}
